/*******************************************************************************
* File Name          : BMP085.c
* Author             : Jon Henry
* Version            : $Revision:$
* Date               : $Date:$
* Description        : BMP085 driver file
*                    : I2C protocols based on Standard Peripherals
*                    : Library Version 1.1.0
*                      
* HISTORY:
* Date               | Modification                    | Author
*
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "bmp085.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
i16_t AC1, AC2, AC3;
u16_t AC4, AC5, AC6;
i16_t B1, B2, MB, MC, MD;
i32_t UT, UP;
/* Private function prototypes -----------------------------------------------*/

/******************************************************************************
* Function Name		: BMP085_ReadReg
* Description		: Reads one byte from register through I2C bus.
* Input				: Register Address
* Input				: Variable to read data to
* Output			: Data Read
* Return			: MEMS_SUCCESS
*******************************************************************************
Master |ST|   | SAD + W |   | REG |     |SR|   | SAD + R |   |   |    |NMAK|SP|
Slave     |EV5|         |EV6|     | EV8 |  |EV5|         |EV6|EV7|DATA|    |  |
*******************************************************************************
*           Transmitter Mode                         Receiver Mode            *
*     SAD + W = Slave address|0x00           SAD + R = Slave Address|0x01     *
******************************************************************************/
status_t BMP085_ReadReg(u8_t Reg, u8_t* Data) {

	//Wait until I2C1 is not busy any more
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

	//Re-enable acknowledge
	I2C_AcknowledgeConfig(I2C1, ENABLE);

	//Send I2C1 START condition
	I2C_GenerateSTART(I2C1, ENABLE);
	//Wait for I2C1 EV5 --> Slave has acknowledged start condition
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

	//Send slave Address for write
	I2C_Send7bitAddress(I2C1, BMP085_I2C_ADDRESS, I2C_Direction_Transmitter);
	//Wait for I2C1 EV6, check if slave has acknowledged Master transmitter
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

	//Send address of register
	I2C_SendData(I2C1, Reg);
	//Wait for I2C1 EV8, check if slave has acknowledged Master Transmitter
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING));

	//Send START condition a second time (Re-Start)
	I2C_GenerateSTART(I2C1, ENABLE);
	//Wait for I2C1 EV5 --> Slave has acknowledged start condition
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

	//Send slave Address for read
	I2C_Send7bitAddress(I2C1, BMP085_I2C_ADDRESS, I2C_Direction_Receiver);

	//Send NMAK
	I2C_AcknowledgeConfig(I2C1, DISABLE);

	//Wait for I2C1 EV6, check if slave has acknowledged Master Receiver
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

	//Wait for I2C1 EV7 --> One byte has been received
	while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) );

	//Read data from I2C data register and return data byte
	*Data = I2C_ReceiveData(I2C1);

	I2C_GenerateSTOP(I2C1, ENABLE);
	//stop bit flag
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));

  return MEMS_SUCCESS;
}

/*******************************************************************************
* Function Name		: BMP085_WriteReg
* Description		: Generic write function.
* Input				: Register Address, Data to be written
* Output			: None
* Return			: None
*******************************************************************************
Master |ST|         |SAD + W|           |REG|           |DATA|             |SP|
Slave        |EV5|              |EV6|          |EV8_2|            |EV5|
*******************************************************************************
*           Transmitter Mode                         Receiver Mode            *
*     SAD + W = Slave address|0x00           SAD + R = Slave Address|0x01     *
******************************************************************************/
status_t BMP085_WriteReg(u8_t Reg, u8_t Data) {

	//I2C Write Sequence

	//Wait until I2C1 is not busy anymore
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

	// Send I2C1 START condition
	I2C_GenerateSTART(I2C1, ENABLE);
	//Wait for I2C1 EV5 --> Slave has acknowledged start condition
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

	//Send slave Address for write
	I2C_Send7bitAddress(I2C1, BMP085_I2C_ADDRESS, I2C_Direction_Transmitter);
	//Wait for I2C1 EV6, check if slave has acknowledged Master transmitter
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

	//Mode register address
	I2C_SendData(I2C1, Reg);
	//wait for byte send to complete
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

	//wait for byte send to complete
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
	//clear bits
	I2C_SendData(I2C1, Data);

	//generate stop
	I2C_GenerateSTOP(I2C1, ENABLE);
	//stop bit flag
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));

	return MEMS_SUCCESS;
}

/******************************************************************************
* Function Name		: BMP085_ReadCalReg
* Description		: Generic Reading function. It must be fulfilled with either
*					: I2C or SPI reading functions
* Input				: Starting Register Address
* Output			: Data Buffer Read
* Return			: None
*******************************************************************************
Master|ST| |SAD + W| |REG|     |SR| |SAD + R|   |   |MAK |   |MAK |   |NMAK|SP|
Slave   |EV5|     |EV6|  |EV8_2| |EV5|      |EV6|EV7|DATA|   |DATA|   |DATA|
*******************************************************************************
*           Transmitter Mode                         Receiver Mode            *
*     SAD + W = Slave address|0x00           SAD + R = Slave Address|0x01     *
******************************************************************************/
status_t BMP085_ReadCalReg(u8_t Reg) {


	u8_t AC1H, AC1L, AC2H, AC2L, AC3H, AC3L, AC4H, AC4L, AC5H, AC5L;
	u8_t AC6H, AC6L, B1H, B1L, B2H, B2L, MBH, MBL ,MCH, MCL, MDH, MDL;


	//Test on BUSY Flag
	while (I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));

	//Re-enable acknowledge
	I2C_AcknowledgeConfig(I2C1, ENABLE);

	//Send I2C1 START condition
	I2C_GenerateSTART(I2C1, ENABLE);
	//Wait for I2C1 EV5 --> Slave has acknowledged start condition
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

	//Send device address for write
	I2C_Send7bitAddress(I2C1, BMP085_I2C_ADDRESS, I2C_Direction_Transmitter);
	//Wait for I2C1 EV6, check if slave has acknowledged Master transmitter
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

	//Send the device's internal address to read from with MSB asserted
	I2C_SendData(I2C1, Reg|0x80);
	//Wait for I2C1 EV8_2, check if slave has acknowledged Master Transmitter
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING));

	//Send START condition a second time (Re-Start)
	I2C_GenerateSTART(I2C1, ENABLE);
	//Wait for I2C1 EV5 --> Slave has acknowledged start condition
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

	//Send address for read
	I2C_Send7bitAddress(I2C1, BMP085_I2C_ADDRESS, I2C_Direction_Receiver);
	//Wait for I2C1 EV6, check if slave has acknowledged Master Receiver
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

	//Load in all 6 registers */
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC1H = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC1L = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC2H = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC2L = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC3H = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC3L = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC4H = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC4L = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC5H = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC5L = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC6H = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	AC6L = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	B1H = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	B1L = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	B2H = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	B2L = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	MBH = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	MBL = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	MCH = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	MCL = I2C_ReceiveData(I2C1);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	MDH = I2C_ReceiveData(I2C1);
	//Disable ACK after last byte read and before actually reading it
	I2C_AcknowledgeConfig(I2C1, DISABLE);
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	I2C_GenerateSTOP(I2C1, ENABLE);
	MDL = I2C_ReceiveData(I2C1);
	//Ensure stop bit was set.
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));

	AC1 = (i16_t)((AC1_H << 8) | AC1_L);
	AC2 = (i16_t)((AC2_H << 8) | AC2_L);
	AC3 = (i16_t)((AC3_H << 8) | AC3_L);
	AC4 = (i16_t)((AC4_H << 8) | AC4_L);
	AC5 = (i16_t)((AC5_H << 8) | AC5_L);
	AC6 = (i16_t)((AC6_H << 8) | AC6_L);
	B1 = (i16_t)((B1_H << 8) | B1_L);
	B2 = (i16_t)((B2_H << 8) | B2_L);
    MB = (i16_t)((MB_H << 8) | MB_L);
	MC = (i16_t)((MC_H << 8) | MC_L);
	MD = (i16_t)((MD_H << 8) | MD_L);
	
	return MEMS_SUCCESS;
}

/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : BMP085_Init
* Description    : Sets BMP085 configuration
* Input          : None
* Output         : None
* Return         : Status [MEMS_ERROR, MEMS_SUCCESS]
*******************************************************************************/
status_t BMP085_Init(){

	BMP085_ReadCalReg(AC1_H);
	
 return MEMS_SUCCESS;
}

/********************************************************************************
* Function Name  : getUT
* Description    : Gets BMP085 Uncompensated Temperature
* Input          : Output Data Rate
* Output         : None
* Return         : Status [MEMS_ERROR, MEMS_SUCCESS]
********************************************************************************/
status_t BMP085_GetUT(){
 

 return MEMS_SUCCESS;
}

/********************************************************************************
* Function Name  : GetUP
* Description    : Gets BMP085 Uncompensated Pressure
* Input          : MEMS_ENABLE, MEMS_DISABLE
* Output         : None
* Return         : Status [MEMS_ERROR, MEMS_SUCCESS]
*********************************************************************************
* TEMP_EN |   (0)   |   (0)   |   DO2   |   DO1   |   DO0   |   (0)   |   (0)   | 
*    0    |    0    |    0    |    1    |    0    |    0    |    0    |    0    |
*********************************************************************************
*  TEMP_EN  |  Temperature Sensor Enable:   0-> Disabled (Default)    1-> Enabled
********************************************************************************/
status_t BMP085_getUP(){


 return MEMS_SUCCESS;
}
